﻿using FirstFloor.ModernUI.Windows.Controls;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using plust.Clases;
using plust.Persistencia;
using System.Diagnostics;
using System.Collections;
using System.Data;
using FirstFloor.ModernUI.Windows;
using System.Threading;

namespace plust.Content
{
    /// <summary>
    /// Interaction logic for ModificarFactura.xaml
    /// </summary>
    public partial class ModificarFactura : UserControl, IContent
    {
        private PersistenciaDB persistencia = new PersistenciaDB();

        public ModificarFactura()
        {
            InitializeComponent();
        }

        private async void cargarFacturas()
        {
            pb.IsIndeterminate = true;

            this.dgModificarFacturas.Visibility = System.Windows.Visibility.Hidden;

            this.pb.Visibility = System.Windows.Visibility.Visible;

            var slowTask = Task<DataTable>.Factory.StartNew(() => cargarFacturasTask());

            await slowTask;

            this.pb.Visibility = System.Windows.Visibility.Hidden;

            pb.IsIndeterminate = false;

            this.dgModificarFacturas.ItemsSource = slowTask.Result.DefaultView;
            
            this.dgModificarFacturas.Visibility = System.Windows.Visibility.Visible;

        }

        private DataTable cargarFacturasTask()
        {
            return persistencia.obtenerFacturas();
        }

        private void dgModificarFactura_Row_DoubleClick(object sender, MouseButtonEventArgs e)
        {

            DetalleFacturaDialog dfd = new DetalleFacturaDialog(Convert.ToInt32(((DataRowView)this.dgModificarFacturas.SelectedItem).Row["id_venta"].ToString()), Convert.ToInt32(((DataRowView)this.dgModificarFacturas.SelectedItem).Row["id_cliente"].ToString()), Convert.ToInt32(((DataRowView)this.dgModificarFacturas.SelectedItem).Row["id_factura"].ToString()));
            new ModernWindow
            {
                Style = (Style)App.Current.Resources["EmptyWindow"],
                Content = dfd,
                Width = 800,
                Height = 600,
                Margin = new Thickness(42),
                Padding = new Thickness(42),
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
                ResizeMode = ResizeMode.NoResize
            }.ShowDialog();

            this.cargarFacturas();
        }

        public void OnFragmentNavigation(FirstFloor.ModernUI.Windows.Navigation.FragmentNavigationEventArgs e)
        {
            
        }

        public void OnNavigatedFrom(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            
        }

        public void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            this.cargarFacturas();
        }

        public void OnNavigatingFrom(FirstFloor.ModernUI.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            
        }
    }
}
